package com.panopset.sf;

import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.AbstractButton;

/**
 * National Language System (NLS) compatible group of mnemonics.
 * 
 * @author Karl Dinwiddie
 * 
 */
public final class MnemonicGroup {

	/**
	 * Mnemonics.
	 */
	private final List<Integer> mncs = new ArrayList<Integer>();

	/**
	 * Find a mnemonic for a title, which has not been used yet.
	 * 
	 * @param button
	 *            AbstractButton to set the mnemonic for.
	 * @return First character in the button text that has not been used yet. If
	 *         none are available, KeyEvent.VK_UNDEFINED is returned.
	 */
	public int findMnemonic(final AbstractButton button) {
		return findMnemonic(button, KeyEvent.VK_UNDEFINED);
	}

	/**
	 * Find a mnemonic for a title, which has not been used yet.
	 * 
	 * @param button
	 *            AbstractButton to set the mnemonic for.
	 * @param preferredMnemonic
	 *            If the preferredMnemonic is available, that one will be used,
	 *            otherwise the first available one will be returned. If the
	 *            text does not contain the preferred mnemonic, the button name
	 *            will be prepended with the preferred mnemonic and a space.
	 * @return Preferred mnemonic, if available, otherwise first character in
	 *         the button text that has not been used yet. If none are
	 *         available, KeyEvent.VK_UNDEFINED is returned.
	 */
	public int findMnemonic(final AbstractButton button,
			final int preferredMnemonic) {
		return preferredMnemonic;
//		// TODO: Java 7.
//		boolean buttonHasMnemonic = false;
//		if (KeyEvent.VK_UNDEFINED != preferredMnemonic
//				&& !mncs.contains(preferredMnemonic)) {
//			for (Character c : button.getText().toCharArray()) {
//				
//				int keyCode = KeyEvent.getExtendedKeyCodeForChar(c);
//				
//				if (preferredMnemonic == keyCode) {
//					buttonHasMnemonic = true;
//					continue;
//				}
//			}
//			if (!buttonHasMnemonic) {
//				button.setText(("" + ((char) preferredMnemonic)).toLowerCase()
//						+ " " + button.getText());
//			}
//		}
//		if (preferredMnemonic == KeyEvent.VK_UNDEFINED) {
//			for (Character c : button.getText().toCharArray()) {
//				int keyCode = KeyEvent.getExtendedKeyCodeForChar(c);
//				if (!mncs.contains(keyCode)) {
//					mncs.add(keyCode);
//					button.setMnemonic(keyCode);
//					return keyCode;
//				}
//			}
//		} else {
//			for (Character c : button.getText().toCharArray()) {
//				int keyCode = KeyEvent.getExtendedKeyCodeForChar(c);
//				if (!mncs.contains(keyCode) && preferredMnemonic == keyCode) {
//					mncs.add(keyCode);
//					button.setMnemonic(keyCode);
//					return keyCode;
//				}
//			}
//			return findMnemonic(button, KeyEvent.VK_UNDEFINED);
//		}
//		return KeyEvent.VK_UNDEFINED;
	}

	/**
	 * If you have a menu bar, add the menu menmonic to the mnemonic group of
	 * each menu item the mnemonic of the menu.
	 * 
	 * @param mnemonic
	 *            Mnemonic to add to this group.
	 */
	public void add(final int mnemonic) {
		mncs.add(mnemonic);
	}
}
